'use strict';

    angular.module('app').controller('userDetailController', ['$scope', '$rootScope', '$state', '$timeout', 'toaster',
                                             function($scope, $rootScope, $state, $timeout, toaster) {
    	
    	$scope.record = {};
    	$scope.record.enable = 1;
        var title = "";
        var defaultAva = $rootScope.defaultAvatar;
        $scope.myImage='';
        // $scope.myCroppedImage=$scope.myCroppedImage ;
        $scope.myCroppedImage = '';
        title="操作员详情";
        
        $scope.title = $rootScope.title = title;
        $scope.loading = true;
        
        var id = $state.params.id;
        $scope.record.id = $state.params.id;
        activate(id);
        


        var handleFileSelect=function(evt) {
            var file=evt.currentTarget.files[0];
            if(!/image\/\w+/.test(file.type)){
                return false;
            }
            var reader = new FileReader();
            reader.onload = function (evt) {
                $scope.$apply(function($scope){
                    $scope.myImage=evt.target.result;
                });
            };
            reader.readAsDataURL(file);
        };
        angular.element(document.querySelector('#fileInput')).on('change',handleFileSelect);
        $('.upload-pic-box a').click(function() {
        	$('#fileInput').click();
        });

        // 初始化页面
        function activate(id) {
	        $scope.loading = true;
        	$.ajax({
        		type: 'PUT',
				url : '/user/read/detail',
	            dataType: 'json',
				contentType:'application/json;charset=UTF-8',
				data: angular.toJson({'id': id})
			}).then(function(result) {
		        $scope.loading = false;
				if (result.httpCode == 200) {
					$scope.record = result.data;
					$scope.record.beginTime = $scope.record.beginTime.substr(0,10);//去掉时分秒
					$scope.record.endTime = $scope.record.endTime.substr(0,10);//去掉时分秒
					$scope.record.password = '';
					$scope.regionName = result.data.regionName;
				} else {
					$scope.msg = result.msg;
				}
				$scope.$apply();
				
				$scope.loadRole();
				
				$scope.loadUserType();
			});
        }

        
        $scope.loadUserType = function () {
    		$scope.param = {};
    		$scope.param.key='USERTYPE';
    		$.ajax({
				type: 'PUT',
	            dataType: 'json',
				contentType:'application/json;charset=UTF-8',
				url : '/dic/queryDicByType',
				data: angular.toJson($scope.param)
			}).then(function(result) {
				if (result.httpCode == 200) {
					var userTypes = result.data;
					for(var key in userTypes){
						if(key == $scope.record.userType){
							$scope.userTypeName = userTypes[key]; 
						}
					}
					$scope.$apply();
				} 
				
			});
    	}
    	
        $scope.loadRole = function () {
    		$scope.param = {};
    		$.ajax({
				type: 'PUT',
	            dataType: 'json',
				contentType:'application/json;charset=UTF-8',
				url : '/role/queryRoles',
				data: angular.toJson($scope.param)
			}).then(function(result) {
				if (result.httpCode == 200) {
					var roles = result.data;
					var role;
					for(var i=0;i<roles.length;i++){
						role = roles[i];
						if(role.id == $scope.record.roleId){
							$scope.roleName = role.roleName;
							break;
						}
					}
					$scope.$apply();
				} 
				
			});
    	}


      //日期控件
        $scope.dateOptions = {
             startingDay: 1,
             class: 'datepicker'
        };
        //日期控件
        $scope.format = "yyyy-MM-dd";
        $scope.openDatepicker = function($event, i) {
	        $event.preventDefault();
	        $event.stopPropagation();
	        if(i==1){
	        	$scope.beginTime = true;
	        }else if(i==2){
	        	$scope.endTime = true;
	        }
        }

    }]);